package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.bean.SelectPicBean
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.utils.ConvertUtil
import com.onlyphoto.onlymall.utils.StringUtil

import me.drakeet.multitype.ItemViewBinder


/**
 * Created by gaochujia on 2019-07-09.
 */
class PicBinder : ItemViewBinder<SelectPicBean, PicBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_pic, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: SelectPicBean) {
        viewHolder.setProgressBean(bean)
        viewHolder.update(bean)
        viewHolder.ivDelete?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View) {
                listener?.delete(viewHolder.adapterPosition)
            }
        })
        viewHolder.itemView.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View) {
                clickEvent?.itemClick(bean, viewHolder.adapterPosition)
            }
        })
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var flPic: FrameLayout? = null
        var pic: ImageView? = null
        var layoutProgress : LinearLayout? = null
        var uploadProgress : ProgressBar? = null
        var tvComplete : TextView? = null
        var tvProgress : TextView? = null
        var ivDelete : ImageView? = null
        private var item: SelectPicBean? = null

        init {
            flPic = itemView.findViewById(R.id.fl_pic)
            pic = itemView.findViewById(R.id.pic)
            layoutProgress = itemView.findViewById(R.id.layout_progress)
            uploadProgress = itemView.findViewById(R.id.progress)
            tvComplete = itemView.findViewById(R.id.tv_complete)
            tvProgress = itemView.findViewById(R.id.tv_progress)
            ivDelete = itemView.findViewById(R.id.iv_delete)
            setIsRecyclable(false)
        }

        fun setProgressBean(bean: SelectPicBean) {
            item = bean
            item?.setOnRefreshCallBack(object :SelectPicBean.OnRefreshCallBack{
                override fun onRefresh() {
                    refreshFileStatus()
                }
            })
        }

        fun refreshFileStatus() {
            layoutProgress?.visibility = View.VISIBLE
            tvProgress?.text = "上传中"
            uploadProgress?.progress = item!!.progress
            item?.type = SelectPicBean.Type_Uploading
            ivDelete?.visibility = View.INVISIBLE
        }
        
        @SuppressLint("SetTextI18n")
        fun update(bean: SelectPicBean) {
            val width = ConvertUtil.getScreenWidth(itemView.context)
            val layoutParams = flPic?.layoutParams
            layoutParams?.width = width / 4
            layoutParams?.height = width / 4
            flPic?.layoutParams = layoutParams
            pic?.setPadding(1, 1, 1, 1)
            if (!StringUtil.isBlank(bean.url)) {
                Glide.with(itemView.context).load(bean.url).into(pic!!)
            }
            when {
                bean.type == SelectPicBean.Type_None -> {
                    layoutProgress?.visibility = View.INVISIBLE
                    ivDelete?.visibility = View.VISIBLE

                }
                bean.type == SelectPicBean.Type_Add -> {
                    layoutProgress?.visibility = View.INVISIBLE
                    Glide.with(itemView.context).load(R.mipmap.add_photo).into(pic!!)
                    ivDelete?.visibility = View.INVISIBLE
                }
                bean.type == SelectPicBean.Type_Uploaded -> {
                    layoutProgress?.visibility = View.VISIBLE
                    uploadProgress?.progress = 100
                    tvComplete?.visibility = View.VISIBLE
                    tvProgress?.visibility = View.GONE
                    uploadProgress?.visibility = View.GONE
                    ivDelete?.visibility = View.INVISIBLE

                }
                bean.type == SelectPicBean.Type_Uploading -> {
                    layoutProgress?.visibility = View.VISIBLE
                    tvComplete?.visibility = View.GONE
                    tvProgress?.visibility = View.VISIBLE
                    uploadProgress?.visibility = View.VISIBLE
                    uploadProgress?.progress = bean.progress
                    tvProgress?.text = "上传中"
                    ivDelete?.visibility = View.INVISIBLE
                }
                bean.type == SelectPicBean.Type_UploadFailed -> {
                    layoutProgress?.visibility = View.VISIBLE
                    tvComplete?.visibility = View.GONE
                    tvProgress?.visibility = View.VISIBLE
                    uploadProgress?.visibility = View.VISIBLE
                    uploadProgress?.progress = bean.progress
                    tvProgress?.text = "上传失败"
                    ivDelete?.visibility = View.VISIBLE
                }
            }
        }
    }

    private var clickEvent: OnItemClickEvent? = null

    fun setOnItemClickEvent(event: OnItemClickEvent) {
        this.clickEvent = event
    }

    interface OnItemClickEvent {
        fun itemClick(bean: SelectPicBean, pos: Int)
    }

    var listener: DeleteSelectPicListener? = null

    interface DeleteSelectPicListener{
        fun delete(position: Int)
    }

    fun setDeleteSelectPicListener(listener: DeleteSelectPicListener){
        this.listener = listener
    }

}
